package com.agile.major.service.impl;

import com.agile.major.domain.BaseMajor;
import com.agile.major.domain.BaseTeacher;
import com.agile.major.repository.BaseMajorRepository;
import com.agile.major.repository.BaseTeacherRepository;
import com.agile.major.service.BaseMajorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Service Implementation for managing BaseMajor.
 */
@Service
@Transactional
public class BaseMajorServiceImpl implements BaseMajorService{

    private final Logger log = LoggerFactory.getLogger(BaseMajorServiceImpl.class);

    private final BaseMajorRepository baseMajorRepository;

    private final BaseTeacherRepository baseTeacherRepository;

    public BaseMajorServiceImpl(BaseMajorRepository baseMajorRepository,BaseTeacherRepository baseTeacherRepository) {
        this.baseMajorRepository = baseMajorRepository;
        this.baseTeacherRepository=baseTeacherRepository;
    }

    /**
     * Save a baseMajor.
     *
     * @param baseMajor the entity to save
     * @return the persisted entity
     */
    @Override
    public BaseMajor save(BaseMajor baseMajor) {
        log.debug("Request to save BaseMajor : {}", baseMajor);
        return baseMajorRepository.save(baseMajor);
    }

    /**
     *  Get all the baseMajors.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<BaseMajor> findAll(Pageable pageable) {
        log.debug("Request to get all BaseMajors");
        return baseMajorRepository.findAll(pageable);
    }

    /**
     *  Get one baseMajor by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public BaseMajor findOne(Long id) {
        log.debug("Request to get BaseMajor : {}", id);
        return baseMajorRepository.findOne(id);
    }

    /**
     *  Delete the  baseMajor by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete BaseMajor : {}", id);
        baseMajorRepository.delete(id);
    }

    @Override
    public Page<BaseMajor> findMajorByTeacher(String teacherCode) throws Exception {
        BaseTeacher teacher=baseTeacherRepository.findBaseTeacherByTeacherCode(teacherCode);
        if(teacher==null){
            throw new Exception("未找到教师，教工号为："+teacherCode);
        }
        return baseMajorRepository.findMajorsByChargeTeacherOrLeaderTeacher(teacher,new PageRequest(0,999));
    }

    @Override
    public Page<BaseMajor> findMajorBySubadmin(String teacherCode) throws Exception {
        BaseTeacher teacher=baseTeacherRepository.findBaseTeacherByTeacherCode(teacherCode);
        if(teacher==null){
            throw new Exception("未找到教师，教工号为："+teacherCode);
        }
        return baseMajorRepository.findMajorsByDepartment(teacher.getDepartment(),new PageRequest(0,999));
    }

    @Override
    public Page<BaseMajor> findMajorBySubadminAndTeacher(String teacherCode) throws Exception {
        BaseTeacher teacher=baseTeacherRepository.findBaseTeacherByTeacherCode(teacherCode);
        if(teacher==null){
            throw new Exception("未找到教师，教工号为："+teacherCode);
        }
        return baseMajorRepository.findMajorsByDepAndTeacher(teacher,teacher.getDepartment(),new PageRequest(0,999));
    }

    @Override
    public Page<BaseMajor> findMajorByTeacherPaper(String teacherNo, Pageable page) throws Exception {
        BaseTeacher teacher=baseTeacherRepository.findBaseTeacherByTeacherCode(teacherNo);
        if(teacher==null){
            throw new Exception("未找到教师，教工号为："+teacherNo);
        }
        return baseMajorRepository.findMajorsByChargeTeacherOrLeaderTeacher(teacher,page);
    }

    @Override
    public Page<BaseMajor> findMajorBySubadminPaper(String teacherNo, Pageable page) throws Exception {
        BaseTeacher teacher=baseTeacherRepository.findBaseTeacherByTeacherCode(teacherNo);
        if(teacher==null){
            throw new Exception("未找到教师，教工号为："+teacherNo);
        }
        return baseMajorRepository.findMajorsByDepartment(teacher.getDepartment(),page);
    }

    @Override
    public Page<BaseMajor> findMajorBySubadminAndTeacherPaper(String teacherNo, Pageable page) throws Exception {
        BaseTeacher teacher=baseTeacherRepository.findBaseTeacherByTeacherCode(teacherNo);
        if(teacher==null){
            throw new Exception("未找到教师，教工号为："+teacherNo);
        }
        return baseMajorRepository.findMajorsByDepAndTeacher(teacher,teacher.getDepartment(),page);
    }
}
